<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        :simple-page="true"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        @search-reset="handleRefreshChange"
        :before-open="handleOpenBefore"
        :data="list"
        @row-update="update"
      >
        <template slot="menuLeft">
          <!-- <el-button
            class="filter-item"
            @click="$refs.crud.rowEdit()"
            type="primary"
            icon="el-icon-edit"
            >修改ID
          </el-button> -->
        </template>

        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row, scope.index)"
            icon="el-icon-edit"
            >修改ID
          </el-button>
          <el-button
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row, scope.index)"
            >查看
          </el-button>
        </template>
        <template slot="status" slot-scope="scope">
          <el-switch
            :value="Boolean(scope.row.status !== 2)"
            @change="changeFlag(scope.row.status, scope.row.id)"
            active-color="#ff4949"
            inactive-color="#13ce66"
          >
          </el-switch>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, updateUser, putObj  } from "@/api/admin/user";
import { deptRoleList } from "@/api/admin/role";
// import {regionList} from "@/api/admin/organization";
import { tableOption } from "@/const/crud/admin/user";
import { mapGetters } from "vuex";

export default {
  name: "table_user",
  data() {
    return {
      option: tableOption,
      treeDeptData: [],
      checkedKeys: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      list: [],
      listLoading: true,
      role: [],
      form: {},
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {
    role() {
      this.form.role = this.role;
    }
  },
  created() {
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        this.list = response.data.data.data;
        for (const item of this.list) {
          if(item.user_no){
            item.__id = item.user_no
          }else{
            item.__id = item.id
          }
        }
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    getNodeData(data) {
      deptRoleList().then(response => {
        this.rolesOptions = response.data.data;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      this.getList(this.page, param);
      done();
    },
    handleRefreshChange() {
      this.getList(this.page);
    },
    handleOpenBefore(show, type) {
      show();
    },
    changeFlag(status, id) {
      let num;
      if (status === 2) {
        num = 3;
      } else {
        num = 2;
      }
      updateUser({ status: num, id: id }).then(res => {
        this.getList(this.page);
        this.$notify.success("操作成功");
      });
    },
    handleUpdate(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    handleView(row, index){
      this.$refs.crud.rowView(row, index);
    },
    create(row, done, loading) {
      if(this.form.role_type === 20){
        this.$notify.error("创建企业账号需要通过【企业管理->创建企业】操作来关联企业信息");
        loading()
        return false
      }
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
  }
};
</script>
